package allod.som

import Lattice.Pattern
import Lattice.NeuronLocation
import Lattice.Neuron

/**
 * Created by IntelliJ IDEA.
 * User: allod
 * Date: 6 січ 2010
 * Time: 19:53:04
 */

object Metric {
  def apply(name: String): Metric = {
    try {
      val functionClass = Class.forName(name)
      functionClass.newInstance.asInstanceOf[Metric]
    } catch {
      case _ => throw new IllegalArgumentException("Unknown function")
    }
  }
}

trait Metric {
  def distance(input: Pattern, weights: Neuron): Double

  def minDistance(input: Pattern, lattice: Lattice): NeuronLocation = {
    var min = (0, 0)
    var minValue = distance(input, lattice.neuronAt(0, 0))
    for{
      i <- 0 until lattice.columns
      j <- 0 until lattice.rows
      val value = distance(input, lattice.neuronAt(i, j))
      if (value < minValue)
    } {
      minValue = value
      min = (i, j)
    }
    min
  }
}